package com.ibm.risk.irmp.base.auth.rest;


import com.ibm.risk.irmp.base.auth.entity.SystemRole;
import com.ibm.risk.irmp.base.auth.entity.SystemRolePermission;
import com.ibm.risk.irmp.base.auth.service.IRoleManagementService;
import com.ibm.risk.irmp.base.auth.vo.RolePermission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/v2/auth/roles")
public class RoleController {
    private static final Logger log = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private IRoleManagementService roleManagementService;

//    @Autowired
//    private IDataSupplementService dataSupplementService;
//
//    @Autowired
//    private IReportDataService reportDataService;

    @GetMapping(value = "/loadRoleList")
    @ResponseBody
    public List<SystemRole> loadRoleList() {

        List<SystemRole> list = roleManagementService.getRoleList();

        return list;
    }

    @GetMapping(value = "/loadFunctionList")
    @ResponseBody
    public List<Map<String, Object>> loadFunctionList(@RequestParam String functionType) {
        List<Map<String, Object>> functionList = Collections.EMPTY_LIST;

        if (SystemRolePermission.ENUM_FUNCTION_TYPE.MENU.getCode().equals(functionType)) {
            //系统功能菜单
            functionList = roleManagementService.showPermission("T");
//        } else if (SystemRolePermission.ENUM_FUNCTION_TYPE.DATA_SUP.getCode().equals(functionType)) {
//            // 数据补录模板
//            List<PSupTemplateEntity> supTemplateList = dataSupplementService.getAllSupTemplateList(null);
//            //树形目录下拉框
//            ArrayList children = new ArrayList();
//            for (PSupTemplateEntity entity : supTemplateList) {
//                HashMap item = new HashMap();
//                item.put("title", entity.getTemplateName());
//                item.put("value", entity.getTemplateSeq());
//                item.put("key", entity.getTemplateSeq());
//                item.put("isLeaf", true);
//                children.add(item);
//            }
//
//            HashMap root = new HashMap();
//            root.put("title", "补录数据模板");
//            root.put("value", "0");
//            root.put("key", "0");
//            root.put("expanded", true);
//            root.put("children", children);
//            functionList.add(root);
//        } else if (SystemRolePermission.ENUM_FUNCTION_TYPE.REPORT.getCode().equals(functionType)) {
//            // 数据报表: 树形目录
//            functionList = reportDataService.getReportListTree(null, "T", null);
        }
        return functionList;
    }

    @GetMapping(value = "/{roleCode}")
    public SystemRole findRoleById(@PathVariable String roleCode) {
        return roleManagementService.findRoleById(roleCode);
//        if ((String)reqJSON.get("id") != null) {
//            String id = (String)reqJSON.get("id");
//            reqJSON.put("ROLE_ID", (String)reqJSON.get("id"));
//            reqJSON.put("ROLE_STATUS", "T");
//            return HashMap.toJSON(roleManagementService.findRoleById(HashMap.toJavaObject(reqJSON, SystemRole.class)));
//        }
//        return null;
    }

    //    @PostMapping(value = "/usermng/role/delRoleById", produces = "application/json;charset=UTF-8")
//    @ResponseBody
    @DeleteMapping(value = "/delRoleById")
    public Boolean delRoleById(@RequestParam String roleId) {

        boolean result = roleManagementService.deleteRole(roleId);
        return result;
    }

    //    @PostMapping(value = "/usermng/role/savePMS", produces = "application/json;charset=UTF-8")
//    @ResponseBody
    @PostMapping(value = "/saveRolePermission")
    public Boolean saveRolePermission(@RequestBody RolePermission rolePermission) {
        String roleCode = rolePermission.getRoleCode();
        String functionType = rolePermission.getFunctionType();
        List<Long> functionIds = rolePermission.getFunctionIds();
        //ArrayList resourceIds = (ArrayList)reqJSON.get("resourceIds");

        boolean result = roleManagementService.saveRolePermission(roleCode, functionType, functionIds);
        return result;
//        System.out.println(reqJSON);
//        if ((String)reqJSON.get("ROLE_ID") != null) {
//            String ROLE_ID = (String)reqJSON.get("ROLE_ID");
//            ArrayList updateList = new ArrayList();
//            ArrayList PMS_LIST = (ArrayList)reqJSON.get("PMS_ID");
//            for (int i = 0; i < PMS_LIST.size(); i++) {
//                HashMap obj = new HashMap();
//                obj.put("ROLE_ID", ROLE_ID);
//                obj.put("PMS_ID", PMS_LIST.get(i));
//                updateList.add(obj);
//            }
//            if (updateList.size() == roleManagementService.editPMS(ROLE_ID, updateList).size()) {
//                return true;
//            }
//        }
//        return false;
    }

    @PostMapping(value = "/searchRolePermission")
    public List<Long> searchRolePermission(@RequestBody HashMap reqJSON) {
        String roleCode = (String) reqJSON.get("roleCode");
        String functionType = (String) reqJSON.get("functionType");
//        if (roleId != null) {
//            ArrayList returnArr = new ArrayList();
//            List<SystemRolePermission> list = roleManagementService.showRolePermissions(roleId);
//            for(int i=0;i<list.size();i++){
//                returnArr.add(list.get(i).getPmsId());
//            }
//            return returnArr;
//        } else {
//            return null;
//        }
        List<SystemRolePermission> list = roleManagementService.showRolePermissions(roleCode, functionType);

        return list.stream().map(SystemRolePermission::getPmsId).toList();
    }

    //    @PostMapping(value = "/usermng/role/saveRole", produces = "application/json;charset=UTF-8")
//    @ResponseBody
    @PostMapping(value = "/saveRole")
    public void saveRole(@RequestBody SystemRole roleEntity) {
        roleManagementService.saveRole(roleEntity);
    }
}
